package com.franklin.ideaplugin.api.utils;

import com.franklin.ideaplugin.easytesting.action.ExecuteMethodAction;
import com.intellij.openapi.diagnostic.Logger;

import java.util.function.Supplier;

/**
 * @author Ye Junhui
 * @since 2023/7/22
 */
public class ExecuteUtils {

    private static final Logger log = Logger.getInstance(ExecuteMethodAction.class);

    /**
     * 忽略异常的动作
     * @param action
     * @param defaultValue
     * @param <T>
     * @return
     */
    public static <T> T ignoreErrorAction(Supplier<T> action,T defaultValue){
        try {
            return action.get();
        } catch (Throwable throwable) {
            //ignore
            log.info(throwable);
            return defaultValue;
        }
    }

    /**
     * 忽略异常动作
     * @param successAction
     * @param failAction
     * @param <T>
     * @return
     */
    public static <T> T ignoreErrorAction(Supplier<T> successAction,Supplier<T> failAction){
        try {
            return successAction.get();
        } catch (Throwable throwable) {
            //ignore
            log.info(throwable);
            return failAction.get();
        }
    }
}
